package com.imooc.controller.manage;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.imooc.entity.Book;
import com.imooc.entity.Category;
import com.imooc.entity.Evaluation;
import com.imooc.exception.BusinessException;
import com.imooc.exception.BusinessExceptionCode;
import com.imooc.service.BookService;
import com.imooc.service.CategoryService;
import com.imooc.service.EvaluationService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/management/evaluation")
public class ManageEvaluationController {
    @Resource
    private EvaluationService evaluationService;

    @GetMapping("/index.html")
    public ModelAndView showBook() {
        return new ModelAndView("/management/evaluation");
    }

    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> evaluations(Integer page, Integer limit) {
        if (page == null) page = 1;
        if (limit == null) limit = 10;
        IPage<Evaluation> evaluationIPage = evaluationService.paging(page, limit);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "success");
        result.put("data", evaluationIPage.getRecords()); // 当前页面数据
        result.put("count", evaluationIPage.getTotal()); // 为分页时记录总数
        return result;
    }

    @PostMapping("/disable")
    @ResponseBody
    public Map<String, Object> disable(Long evaluationId, String reason) {
        Map<String, Object> result = new HashMap<>();
        try {
            evaluationService.disableEvaluation(evaluationId, reason);
            result.put("code", 0);
            result.put("msg", "success");
        } catch (BusinessException ex) {
            ex.printStackTrace();
            result.put("code", ex.getCode());
            result.put("msg", ex.getMessage());
        }
        return result;
    }
}
